<template>
    <div>
      <el-dialog class="zczyXdChangeDw" title="中压注册线段单位更换" :visible.sync="show" v-if="show" width="30%" :before-close="handleCancel" :close-on-click-modal="false" v-dialogDrag>
        <el-form :model="changeDwForm" ref="changeDwForm" label-width="110px">
          <el-form-item label="原单位编码">
            <el-input v-model="oldDwbm" disabled></el-input>
          </el-form-item>
          <el-form-item label="原单位名称">
            <el-input v-model="oldDwmc" disabled></el-input>
          </el-form-item>
          <el-form-item label="变更日期">
            <el-date-picker v-model="changeDwForm.zcZyxdsj.zcrq" type="date" :picker-options="pickerDate" value-format="yyyy-MM-dd" placeholder="请选择" :clearable="false"></el-date-picker>
          </el-form-item>
          <el-form-item label="新单位编码">
            <el-row>
              <el-col :span="16">
                <el-input v-model="changeDwForm.zcZyxdsj.dwbm" disabled></el-input>
              </el-col>
              <el-col :span="8" class="selectDwbm">
                <el-button type="primary" @click="selectDwbm">选择</el-button>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="新单位名称">
            <el-input v-model="changeDwForm.zcZyxdsj.dwmc" disabled></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="medium" @click="handleCancel">取 消</el-button>
          <el-button size="medium" type="primary" @click="handleSubmit">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 选择单位编码 -->
      <GetDwbm :treeObj="treeObj" ref="getDwbmRef" @handleGetDwbm="handleGetDwbm"></GetDwbm>
    </div>
</template>
<script>
import { changeZczyXd, checkZczyXdbm } from '@/api/sjwh/zczy.js';
import GetDwbm from "@/dialogCommom/GetDwbm";
import { DateDifference } from '@/utils/validate.js';
import { getPreDate } from '@/utils/moment.js';
export default {
    data() {
        return {
          show: false,
          changeDwForm: {
            bglx: 'ghdw',
            zcZyxdsj: {
              dwbm: '',
              dwmc: '',
              zcrq: '',
              id: '',
              czy: ''
            },
            zcZyxdsjValidCxkgVO: {
              cxkg: '',
              dwbm: '',
              oid: '',
              xdbm: ''
            },
            zcZyyhsjValidXdZxrqVO: {
              dwbm: '',
              xdId: [],
              zxrq: ''
            },
            yxZytdxdValidXdZxrqVO: {
              dwbm: '',
              xdId: [],
              zxrq: ''
            }
          },
          oldDwbm: '', // 原单位编码
          oldDwmc: '', // 原单位名称
          oldZcrq: '', // 线段的原注册日期
          len: 0, // 该线路下线段的条数
          treeObj: { // 传给选择单位编码组件的值
            type: 'radio',
            dwbm: ''
          },
          pickerDate: { // 变更日期选择不能超过当前日期
            disabledDate: time => {
                return (
                    time.getTime() > new Date()
                );
            }
          }
        }
    },
    props: {
      propChangeDwData: {
        type: Object,
        default: function() {
          return {};
        }
      }
    },
    watch: {
      propChangeDwData: {
        deep: true,
        handler(newVal, oldVal) {
          if (newVal.show) {
            const that = this
            that.show = newVal.show
            that.oldDwbm = newVal.dwbm
            that.oldDwmc = newVal.dwmc
            that.oldZcrq = newVal.zcrq
            that.len = newVal.len
            that.changeDwForm.zcZyxdsj.id = newVal.id
            that.changeDwForm.zcZyxdsj.czy = newVal.czy
            that.changeDwForm.zcZyxdsjValidCxkgVO.oid = newVal.id
            that.changeDwForm.zcZyyhsjValidXdZxrqVO.xdId = [newVal.id]
            that.changeDwForm.yxZytdxdValidXdZxrqVO.xdId = [newVal.id]
            that.changeDwForm.zcZyxdsj.czy = newVal.czy
            that.changeDwForm.zcZyxdsjValidCxkgVO.cxkg = newVal.cxkg
            that.changeDwForm.zcZyxdsjValidCxkgVO.xdbm = newVal.xdbm
          }
        }
      }
    },
    components: {
      GetDwbm
    },
    methods: {
      /* 取消 */
      handleCancel() {
        const that = this
        that.show = false
        that.changeDwForm = {
          bglx: 'ghdw',
          zcZyxdsj: {
            dwbm: '',
            dwmc: '',
            zcrq: '',
            id: '',
            czy: ''
          },
          zcZyxdsjValidCxkgVO: {
            cxkg: '',
            dwbm: '',
            oid: '',
            xdbm: ''
          },
          zcZyyhsjValidXdZxrqVO: {
            dwbm: '',
            xdId: [],
            zxrq: ''
          },
          yxZytdxdValidXdZxrqVO: {
            dwbm: '',
            xdId: [],
            zxrq: ''
          }
        }
        that.oldDwbm = ''
        that.oldDwmc = ''
      },

      /* 确定 */
      async handleSubmit() {
        const that = this
        if (!that.changeDwForm.zcZyxdsj.zcrq) {
          that.$message.warning('请选择变更日期')
          return false
        }
        if (DateDifference(that.changeDwForm.zcZyxdsj.zcrq, that.oldZcrq) < 2) {
          that.$message.warning(`线段的变更日期须大于其注册日期(${that.oldZcrq})2日`)
          return false
        }
        if (!that.changeDwForm.zcZyxdsj.dwbm) {
          that.$message.warning('请选择新单位编码')
          return false
        }
        that.changeDwForm.zcZyxdsjValidCxkgVO.dwbm = that.changeDwForm.zcZyxdsj.dwbm
        that.changeDwForm.zcZyyhsjValidXdZxrqVO.dwbm = that.changeDwForm.zcZyxdsj.dwbm
        that.changeDwForm.zcZyyhsjValidXdZxrqVO.zxrq = getPreDate(that.changeDwForm.zcZyxdsj.zcrq)
        that.changeDwForm.yxZytdxdValidXdZxrqVO.dwbm = that.changeDwForm.zcZyxdsj.dwbm
        that.changeDwForm.yxZytdxdValidXdZxrqVO.zxrq = getPreDate(that.changeDwForm.zcZyxdsj.zcrq)
        let res = await changeZczyXd(that.changeDwForm)
        if (res.code == 0 && res.data) {
          that.$message.success('中压注册线段更换单位成功')
          that.handleCancel()
          if (that.len > 1) {
            that.$emit("updateZczyXdTable", 'updateSelf')
          } else {
            that.$emit("updateZczyXdTable", 'updateParent')
          }
        } else {
          that.$message.error(res.message)
        }
      },

      /* 选择新单位编码 */
      selectDwbm() {
        const that = this
        that.treeObj.dwbm = that.oldDwbm
        that.$refs.getDwbmRef.openDwbm()
      },

      /* 单位编码组件回传 */
      handleGetDwbm(val) {
        const that = this
        if (val.data.dwbm == that.oldDwbm) {
          that.$message.warning('不能选择原单位编码')
          return false
        }
        let vo = {
            dwbm: val.data.dwbm,
            xdbm: that.changeDwForm.zcZyxdsjValidCxkgVO.xdbm,
            zcrq: that.oldZcrq
        }
        checkZczyXdbm(vo).then(res => {
            if (res.code == 0 && res.data) {
              that.changeDwForm.zcZyxdsj.dwbm = val.data.dwbm
              that.changeDwForm.zcZyxdsj.dwmc = val.data.dwmc
            } else {
              that.$message.warning('新单位下已有该线段编码')
            }
        })
      }
    }
}
</script>
<style lang="scss" scoped>
.zczyXdChangeDw /deep/ .el-input-number {
  width: 100%;
}
.zczyXdChangeDw /deep/ .el-input-number .el-input__inner {
  text-align: left;
}
.zczyXdChangeDw /deep/ .el-date-editor.el-input {
    width: 100%;
}
/deep/ .el-dialog__header {
  padding: 4px 10px 4px;
}
/deep/ .el-dialog__headerbtn {
  top: 10px;
}
/deep/.el-table--scrollable-x .el-table__body-wrapper {
  z-index: 1;
}
/deep/ .el-dialog__footer {
  padding: 5px 10px 5px;
}
.selectDwbm {
  padding-left: 10px;
}
.selectDwbm /deep/ .el-button--primary {
  width: 100%;
}    
/deep/ .el-dialog__body {
  padding: 10px 20px 1px;
}
</style>
